/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FinderOption.java
 *
 * Created on 20/06/2011, 09:01:52 AM
 */

package GUI.ATTENDANT;

import Procedures.DBConection;
import entities.Asignature;
import entities.Computer;
import entities.ComputerRoom;
import entities.Especiality;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author SIX-14
 */
public class FinderOption extends javax.swing.JPanel {

    /** Creates new form FinderOption */
    public FinderOption(DBConection myConection, int action, JFrame paternFrame, int option, String instruction) {
        initComponents();
        this.myConection=myConection;
        this.action=action;
        this.paternFrame=paternFrame;
        this.option=option;
        jLabel1.setText(instruction);
    }

     private DBConection myConection;
    private int action;
    private JFrame paternFrame;
    private int option;

    public int getOption() {
        return option;
    }

    public void setOption(int option) {
        this.option = option;
    }

    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }


    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public void addPanel(JPanel panel){
        getPaternFrame().getContentPane().removeAll();
            getPaternFrame().add(panel);
            getPaternFrame().pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jButton1.setText("Buscar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel1.setText("                ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(186, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(197, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(!jTextField1.getText().isEmpty()){
        if(getOption()==1){
            ComputerRoom computerRoom = new ComputerRoom();
            getMyConection().sendSQLQuery("SELECT * FROM COMPUTER_ROOM  WHERE  LETTER='"+jTextField1.getText()+"';");
            computerRoom.setLetter(getMyConection().getLastResultSetColumnStrings(1)[0]);
            computerRoom.setActive(Boolean.parseBoolean(getMyConection().getLastResultSetColumnStrings(2)[0]));
            addPanel(new ComputerRoomGui(getMyConection(), getAction(), computerRoom));
        }else if(getOption()==2){
            Computer computer = new Computer();
            getMyConection().sendSQLQuery("SELECT * FROM COMPUTER WHERE NUMBER="+jTextField1.getText()+";");
            computer.setNumber(Integer.parseInt(getMyConection().getLastResultSetColumnStrings(1)[0]));
            computer.setLetterComputerRoom(getMyConection().getLastResultSetColumnStrings(2)[0]);
            computer.setState(getMyConection().getLastResultSetColumnStrings(3)[0]);
            computer.setIp(getMyConection().getLastResultSetColumnStrings(4)[0]);
            computer.setPort(Integer.parseInt(getMyConection().getLastResultSetColumnStrings(5)[0]));
            addPanel(new ComputerGui(getMyConection(), getAction(), computer, getPaternFrame()));
        }else if(getOption()==3){
            Asignature asignature = new Asignature();
            getMyConection().sendSQLQuery("SELECT * FROM ASIGNATURE WHERE ID='"+jTextField1.getText()+"';");
            asignature.setId(myConection.getLastResultSetColumnStrings(1)[0]);
            asignature.setName(getMyConection().getLastResultSetColumnStrings(2)[0]);
            asignature.setEspeciality(getMyConection().getLastResultSetColumnStrings(3)[0]);
            asignature.setActive(Boolean.parseBoolean(getMyConection().getLastResultSetColumnStrings(4)[0]));
            addPanel(new AsignatureGui(getMyConection(), getAction(), asignature));
        }else if(getOption()==4){
            Especiality especiality = new Especiality();
            getMyConection().sendSQLQuery("SELECT * FROM ESPECIALITY WHERE CODE ='"+jTextField1.getText()+"';");
            especiality.setCode(getMyConection().getLastResultSetColumnStrings(1)[0]);
            especiality.setName(getMyConection().getLastResultSetColumnStrings(2)[0]);
            especiality.setActive(Boolean.parseBoolean(getMyConection().getLastResultSetColumnStrings(3)[0]));
            addPanel(new EspecialityGui(getMyConection(), getAction(), especiality));
        }
        }
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
